#include "modifydata.h"

static GtkWidget *id_entry = NULL;
static GtkWidget *name_entry = NULL;
static GtkWidget *addr_entry = NULL;
static GtkWidget *sex_entry = NULL;

char *select_id = NULL;

static void show_data(GtkWidget *list);
static void add_data(GtkWidget *widget, gpointer data);

void select_row_callback(GtkWidget *clist, gint row, gint column, GdkEventButton *event, gpointer data) /*设置select_row信号的回调函数*/
{
    gtk_clist_get_text(GTK_CLIST(clist), row, 0, &select_id);
    printf("%s\n", select_id);
}

static void add_data(GtkWidget *widget, gpointer data)
{
    char *id = (char *)gtk_entry_get_text((GtkEntry *)id_entry);
    char *name = (char *)gtk_entry_get_text((GtkEntry *)name_entry);
    char *addr = (char *)gtk_entry_get_text((GtkEntry *)addr_entry);
    char *sex = (char *)gtk_entry_get_text((GtkEntry *)sex_entry);

    if (strlen(id) != 0 && strlen(name) != 0 && strlen(addr) != 0 && strlen(sex) != 0)
    {
        sqlite3 *db;
        int ret = sqlite3_open("Data.db", &db);
        char sql[100];
        char *errmsg = NULL;
        sprintf(sql, "insert into persons values(%s,'%s','%s','%s');", id, name, addr, sex);
        ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
        if (ret != SQLITE_OK)
        {
            printf("insert fail\n");
        }
        else
            printf("insert success\n");

        ret = sqlite3_close(db);
    }
    else
    {
        show_error(NULL, "错误", "输入信息为空");
        printf("error\n");
    }
    show_data((GtkWidget *)data);
}

static void del_data(GtkWidget *widget, gpointer data)
{
    if (select_id != NULL)
    {

        sqlite3 *db;
        int ret = sqlite3_open("Data.db", &db);
        char sql[100];
        char *errmsg = NULL;
        sprintf(sql, "delete from persons where id=%s;", select_id);
        ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
        if (ret != SQLITE_OK)
            printf("delete fail\n");
        else
            printf("felete success\n");

        ret = sqlite3_close(db);
    }
    else
    {
        printf("error\n");
    }

    show_data((GtkWidget *)data);
}

static void show_data(GtkWidget *list)
{
    sqlite3 *db;
    int ret = sqlite3_open("Data.db", &db);
    char **result, *errmsg;
    int nrow, ncolumn, i, j, index;

    if (sqlite3_get_table(db, "select * from persons order by id;", &result, &nrow, &ncolumn, &errmsg) != 0)
    {
        printf("error : %s\n", errmsg);
        sqlite3_free(errmsg);
    }

    // 清空原本列表中的数据
    gtk_clist_clear((GtkCList *)list);
    index = ncolumn;
    for (i = 0; i < nrow; i++)
    {
        gtk_clist_append((GtkCList *)list, result + index);
        for (j = 0; j < ncolumn; j++)
        {
            printf("%-8s", result[index]);
            index++;
        }
        printf("\n");
    }
    sqlite3_free_table(result);
    ret = sqlite3_close(db);

    return;
}

GtkWidget *create_modifydata_window()
{
    window_modifydata = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    // 显示窗口标题
    gtk_window_set_title((GtkWindow *)window_modifydata, "StuMgt学生信息管理系统");
    // 显示窗口大小
    gtk_widget_set_size_request(window_modifydata, 800, 600);
    // 窗口位置设置：总是居中
    gtk_window_set_position((GtkWindow *)window_modifydata, GTK_WIN_POS_CENTER_ALWAYS);
    // 窗口销毁 即程序结束
    g_signal_connect(window_modifydata, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    /*--------------------------------容器---------------------------*/

    // 创建容器大小为1024*600
    GtkWidget *table = gtk_table_new(600, 800, TRUE);
    // 容器加入窗口
    gtk_container_add(GTK_CONTAINER(window_modifydata), table);

    /*-------------------------------列表操作------------------------*/
    // 设置list显示数据
    int list_x = 200;
    int list_y = 50;
    int list_width = 550;
    int list_height = 400;
    char *title[] = {"id", "name", "addr", "sex"};
    GtkWidget *list = gtk_clist_new_with_titles(4, title);
    gtk_clist_set_column_width((GtkCList *)list, 0, 130);
    gtk_clist_set_column_width((GtkCList *)list, 1, 130);
    gtk_clist_set_column_width((GtkCList *)list, 2, 130);
    gtk_clist_set_column_width((GtkCList *)list, 3, 130);
    gtk_table_attach_defaults(GTK_TABLE(table), list, list_x, list_x + list_width, list_y, list_y + list_height);

    gtk_signal_connect(GTK_OBJECT(list), "select_row", GTK_SIGNAL_FUNC(select_row_callback), NULL);
    // gtk_signal_connect(GTK_OBJECT(clist),”select_row”, GTK_SIGNAL_FUNC(select_row_callback), NULL); /*捕捉select_row信号*/

    // 显示数据
    show_data(list);

    /*-------------------------------按钮操作------------------------*/
    // 设置按钮位置
    int start_x = 50;
    int end_x = 150;
    int stert_y = 50;
    int end_y = 80;

    // 显示数据按钮
    GtkWidget *add_btn = gtk_button_new_with_label("添加");
    // 把按钮加入布局
    gtk_table_attach_defaults(GTK_TABLE(table), add_btn, start_x, end_x, stert_y, end_y);
    // 绑定信号
    g_signal_connect(add_btn, "clicked", G_CALLBACK(add_data), list);

    // 修改数据按钮
    GtkWidget *del_btn = gtk_button_new_with_label("删除");
    // 把按钮加入布局
    gtk_table_attach_defaults(GTK_TABLE(table), del_btn, start_x, end_x, stert_y + 100, end_y + 100);
    // 绑定信号
    g_signal_connect(del_btn, "clicked", G_CALLBACK(del_data), list);

    // 返回按钮
    GtkWidget *find_btn = gtk_button_new_with_label("查找");
    // 把按钮加入布局
    gtk_table_attach_defaults(GTK_TABLE(table), find_btn, start_x, end_x, stert_y + 200, end_y + 200);
    // 绑定信号
    // g_signal_connect(addr_sort_btn, "clicked", G_CALLBACK(addr_sort), list);

    // 返回按钮
    GtkWidget *updata_btn = gtk_button_new_with_label("修改");
    // 把按钮加入布局
    gtk_table_attach_defaults(GTK_TABLE(table), updata_btn, start_x, end_x, stert_y + 300, end_y + 300);
    // 绑定信号
    // g_signal_connect(addr_sort_btn, "clicked", G_CALLBACK(addr_sort), list);

    // 返回按钮
    GtkWidget *drop_btn = gtk_button_new_with_label("清空");
    // 把按钮加入布局
    gtk_table_attach_defaults(GTK_TABLE(table), drop_btn, start_x, end_x, stert_y + 400, end_y + 400);
    // 绑定信号
    // g_signal_connect(addr_sort_btn, "clicked", G_CALLBACK(addr_sort), list);

    // 返回按钮
    GtkWidget *go_back_btn = gtk_button_new_with_label("返回");
    // 把按钮加入布局
    gtk_table_attach_defaults(GTK_TABLE(table), go_back_btn, start_x, end_x, stert_y + 500, end_y + 500);
    // 返回窗口
    g_signal_connect(go_back_btn, "clicked", G_CALLBACK(hide_window), window_modifydata);
    g_signal_connect(go_back_btn, "clicked", G_CALLBACK(show_window), "window_operation");

    /*-------------------------------行编辑、标签------------------------*/

    int entry_x = 260;
    int entry_y = 480;
    int label_width = 50;
    int label_height = 30;
    int entry_width = 180;
    int entry_hight = 30;
    // 设置行编辑控件
    id_entry = gtk_entry_new();
    // 放入固定容器
    gtk_table_attach_defaults(GTK_TABLE(table), id_entry, entry_x, entry_x + entry_width, entry_y, entry_y + entry_hight);
    // 定义字符标签
    GtkWidget *id_label = gtk_label_new("id：");
    // 把字符串放进容器中
    gtk_table_attach_defaults(GTK_TABLE(table), id_label, entry_x - 50, entry_x, entry_y, entry_y + entry_hight);
    // 设置控件初始文字为空
    gtk_entry_set_text(GTK_ENTRY(id_entry), NULL);
    // 设置控件可编辑
    gtk_editable_set_editable((GtkEditable *)id_entry, TRUE);

    // 设置行编辑控件
    name_entry = gtk_entry_new();
    // 放入固定容器
    gtk_table_attach_defaults(GTK_TABLE(table), name_entry, entry_x + 250, entry_x + entry_width + 250, entry_y, entry_y + entry_hight);
    // 定义字符标签
    GtkWidget *name_label = gtk_label_new("name：");
    // 把字符串放进容器中
    gtk_table_attach_defaults(GTK_TABLE(table), name_label, entry_x + 250 - 50, entry_x + 250, entry_y, entry_y + entry_hight);
    // 设置控件初始文字为空
    gtk_entry_set_text(GTK_ENTRY(name_entry), NULL);
    // 设置控件可编辑
    gtk_editable_set_editable((GtkEditable *)name_entry, TRUE);

    // 设置行编辑控件
    addr_entry = gtk_entry_new();
    // 放入固定容器
    gtk_table_attach_defaults(GTK_TABLE(table), addr_entry, entry_x, entry_x + entry_width, entry_y + 50, entry_y + entry_hight + 50);
    // 定义字符标签
    GtkWidget *addr_label = gtk_label_new("addr：");
    // 把字符串放进容器中
    gtk_table_attach_defaults(GTK_TABLE(table), addr_label, entry_x - 50, entry_x, entry_y + 50, entry_y + entry_hight + 50);
    // 设置控件初始文字为空
    gtk_entry_set_text(GTK_ENTRY(addr_entry), NULL);
    // 设置控件可编辑
    gtk_editable_set_editable((GtkEditable *)addr_entry, TRUE);

    // 设置行编辑控件
    sex_entry = gtk_entry_new();
    // 放入固定容器
    gtk_table_attach_defaults(GTK_TABLE(table), sex_entry, entry_x + 250, entry_x + entry_width + 250, entry_y + 50, entry_y + entry_hight + 50);
    // 定义字符标签
    GtkWidget *sex_label = gtk_label_new("sex：");
    // 把字符串放进容器中
    gtk_table_attach_defaults(GTK_TABLE(table), sex_label, entry_x + 250 - 50, entry_x + 250, entry_y + 50, entry_y + entry_hight + 50);
    // 设置控件初始文字为空
    gtk_entry_set_text(GTK_ENTRY(sex_entry), NULL);
    // 设置控件可编辑
    gtk_editable_set_editable((GtkEditable *)sex_entry, TRUE);

    /*-------------------------------sqlite3数据库操作--------------------------*/

    return window_modifydata;
}